package com.resou.reader.bookshelf.dialogutil;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.base.ResouBaseDialog;
import com.resou.reader.mine.login.LoginFragment;

import butterknife.BindView;

public class FreeBookClickDialog extends ResouBaseDialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.iv_dismiss)
    ImageView ivDismiss;

    public static FreeBookClickDialog newInstance() {
        Bundle args = new Bundle();
        FreeBookClickDialog fragment = new FreeBookClickDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_free_book_clicked;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Window win = getDialog().getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        if (win != null) {
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        win.setLayout((dm.widthPixels), (int) (dm.heightPixels * 0.5));
            mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams params = win.getAttributes();
            params.width = (int) (dm.widthPixels * 0.8);
            win.setAttributes(params);
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvLogin.setOnClickListener(v -> {
            LoginFragment fragment = LoginFragment.getInstance();

            fragment.show(getFragmentManager(), "login_fragment");
            dismiss();
        });
        ivDismiss.setOnClickListener(v -> dismiss());
    }

}
